package com.devops.service.impl;

import com.devops.entity.po.OrderInfoPO;
import com.devops.mapper.TestDevopsMapper;
import com.devops.service.TestDevopsService;
import com.devops.utils.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: mis_wu
 * @Company: asia info
 * @Create_time: 2023-11-10
 * @Description: 自定义描述
 */
@Service
public class TestDevopsImpl implements TestDevopsService {

    @Resource
    private TestDevopsMapper testDevopsMapper;

    @Override
    public List<OrderInfoPO> queryOrderList() {
        return testDevopsMapper.queryOrderList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchInsert(List<OrderInfoPO> orderInfos) {
        return testDevopsMapper.batchInsert(orderInfos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchUpdate(List<OrderInfoPO> orderInfos) {
        return testDevopsMapper.batchUpdate(orderInfos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<String> testTransactional(String name) {
        ResponseResult<String> result = new ResponseResult<>();
        try {
            test(name);
        }catch (Exception e){
            result.setCode(9999);
            result.setMsg(e.getMessage());
            result.setSuccess(false);
            throw new RuntimeException(e.getMessage());
        }finally {
            List<OrderInfoPO> orderList = new ArrayList<>();
            OrderInfoPO order = new OrderInfoPO(null,"我的锅","code-55",0);
            orderList.add(order);
            testDevopsMapper.batchInsert(orderList);
        }

        return result;
    }

    private void test(String name) {
        List<OrderInfoPO> orderList = new ArrayList<>();
        OrderInfoPO order = new OrderInfoPO(null,name,"code-44",0);
        orderList.add(order);
        testDevopsMapper.batchInsert(orderList);
        Integer a = 10 / 0;
    }
}
